-- Class Platform --
Game.Platform = {}
Game.Platform.Table = {}
function Game.Platform.initPlatform (screen, x, y)
  Game.Platform.scr = screen
  Game.Platform.x = x
  Game.Platform.y = y
end
function Game.Platform.newHero (x, y, w, h, dest, vit, img)
  local anims = {
  {0, 1, 2, 1},
  {3, 4, 5, 4},
  {6, 7, 8, 7},
  {9, 10, 11, 10}}
  local vitesses = {vit, vit, vit, vit}
  Game.Platform.Table["_hero_"] = {}
  Game.Platform.Table["_hero_"].isJumping = false
  Game.Platform.Table["_hero_"].width = w
  Game.Platform.Table["_hero_"].height = h
  --player.y = 230
  --player.x = 50
  Game.Platform.Table["_hero_"].jumpspeed = 10
  Game.Sprite.new (img, w, h, dest, anims, vitesses, Game.Platform.scr, x, y-1, "_hero_", 2)
  Game.Platform.Table["_hero_"].gravity = y
  Game.Platform.Table["_hero_"].yfix = y
end
function Game.Platform.drawHero ()
  Game.Sprite.draw ("_hero_")
end
function Game.Platform.initHeroCommands (px)
  Collisions.initCollision ()
  gmax = Game.Platform.Table["_hero_"].yfix
  dt_co = CO["_hero_"].inCollisionWith
  if not Collisions.isInCollision ("_hero_") then
    gmax = Game.Platform.Table["_hero_"].yfix
    d_co = "Not in collision"
    --Console.print ("not in collision", 1)
  else
    d_co = tostring (CO[Collisions.nameObject ("_hero_")].y)
    --Console.print ("in collision", 2)
  end
  if Keys.held.Right and not Game.Platform.Table["_hero_"].isJumping then
    if Collisions.mode ("_hero_") ~= "right" then
      Game.Sprite.moveRight ("_hero_", px)
    end
    --Console.print ("moveRight on floor", 3)
  elseif Keys.held.Left and not Game.Platform.Table["_hero_"].isJumping then
    if Collisions.mode ("_hero_") ~= "left" then
      Game.Sprite.moveLeft ("_hero_", px)
    end
    --Console.print ("moveLeft on floor", 4)
  elseif Keys.held.Right and Game.Platform.Table["_hero_"].isJumping then
    Game.Sprite.Table["_hero_"].x = Game.Sprite.Table["_hero_"].x+px
    Game.Sprite.Table["_hero_"].x2 = Game.Sprite.Table["_hero_"].x2+px
    Game.Sprite.Table["_hero_"].dir = 1
    --Console.print ("moveRight in Jump", 5)
  elseif Keys.held.Left and Game.Platform.Table["_hero_"].isJumping then
    Game.Sprite.Table["_hero_"].x = Game.Sprite.Table["_hero_"].x-px
    Game.Sprite.Table["_hero_"].x2 = Game.Sprite.Table["_hero_"].x2-px
    Game.Sprite.Table["_hero_"].dir = 3
    --Console.print ("moveLeft in Jump", 6)
  end
  if Keys.newPress.A and not Game.Platform.Table["_hero_"].isJumping then
    Game.Platform.Table["_hero_"].isJumping = true
    --Game.Sprite.Table["_hero_"].dir = Game.Sprite.Table["_hero_"].dir-1
    --Console.print ("Jump", 7)
  end
  if Collisions.mode ("_hero_") == "down" then
    gmax = CO[Collisions.nameObject ("_hero_")].y-Game.Platform.Table["_hero_"].height
    d_co = tostring (CO[Collisions.nameObject ("_hero_")].y)
    Game.Platform.Table["_hero_"].jumpspeed = 10
    Game.Platform.Table["_hero_"].isJumping = false
    gmax = CO[Collisions.nameObject ("_hero_")].y-Game.Platform.Table["_hero_"].height
    Game.Platform.Table["_hero_"].gravity = gmax
    --Console.print ("Collision down", 8)
  end
  if Game.Platform.Table["_hero_"].isJumping then
    Game.Platform.Table["_hero_"].jumpspeed = Game.Platform.Table["_hero_"].jumpspeed - 0.5
    Game.Platform.Table["_hero_"].gravity = Game.Platform.Table["_hero_"].gravity - Game.Platform.Table["_hero_"].jumpspeed
    --Console.print ("isJumping", 9)
  end
  if Game.Platform.Table["_hero_"].gravity == gmax then
    Game.Platform.Table["_hero_"].jumpspeed = 10
    Game.Platform.Table["_hero_"].isJumping = false
    --Console.print ("gravity == gmax", 10)
  end
  if Game.Platform.Table["_hero_"].gravity > gmax then
    Game.Platform.Table["_hero_"].gravity = gmax
    --Console.print ("gravity > gmax", 11)
  end
  Game.Sprite.Table["_hero_"].y = Game.Platform.Table["_hero_"].gravity
  Game.Sprite.Table["_hero_"].y2 = Game.Sprite.Table["_hero_"].y+Game.Platform.Table["_hero_"].height
end
function Game.Platform.reInitPositions ()
  Game.Platform.Table["_hero_"].gravity = 100
  Game.Platform.Table["_hero_"].jumpspeed = 10
  Game.Platform.Table["_hero_"].isJumping = false
end
function Game.Platform.debug ()
  screen.print (SCREEN_DOWN, 0, 0, "Is in collision = "..d_co)
  screen.print (SCREEN_DOWN, 0, 10, "Gravity = "..tostring (Game.Platform.Table["_hero_"].gravity))
  screen.print (SCREEN_DOWN, 0, 20, "Is jumping = "..tostring (Game.Platform.Table["_hero_"].isJumping))
  screen.print (SCREEN_DOWN, 0, 30, "Jumpspeed = "..tostring (Game.Platform.Table["_hero_"].jumpspeed))
  screen.print (SCREEN_DOWN, 0, 40, "Yfix = "..tostring (Game.Platform.Table["_hero_"].yfix))
  screen.print (SCREEN_DOWN, 0, 50, "Gmax = "..tostring (gmax))
  screen.print (SCREEN_DOWN, 0, 60, "Hero X = "..tostring (Game.Sprite.Table["_hero_"].x))
  screen.print (SCREEN_DOWN, 0, 70, "Hero X2 = "..tostring (Game.Sprite.Table["_hero_"].x2))
  screen.print (SCREEN_DOWN, 0, 80, "Hero Y = "..tostring (Game.Sprite.Table["_hero_"].y))
  screen.print (SCREEN_DOWN, 0, 90, "Hero Y2 = "..tostring (Game.Sprite.Table["_hero_"].y2))
  screen.print (SCREEN_DOWN, 0, 100, "In collision with = "..tostring (dt_co))
  screen.print (SCREEN_DOWN, 0, 110, "Collision Mode = "..tostring (Collisions.mode ("_hero_")))
  function Game.Platform.debugRects (name)
    if not Game.Platform.Table[name].passable then
      Collisions.drawDebugRect (name, c_blanc)
    else
      screen.drawRect (SCREEN_UP, Game.Platform.Table[name].x, Game.Platform.Table[name].y, Game.Platform.Table[name].x2, Game.Platform.Table[name].y2, c_blanc)
    end
  end
end
function Game.Platform.newBlock (x, y, img, name, passable, downpassable)
  local x2 = x+Image.width (img)
  local y2 = y+Image.height (img)
  if passable == nil then passable = false end
  if downpassable == nil then downpassable = false end
  if not passable then
    Collisions.newObject (name, Game.Platform.scr, Game.Platform.x+x, Game.Platform.y+y, Game.Platform.x+x2, Game.Platform.y+y2, "Image")
  end
  Game.Platform.Table[name] = {}
  Game.Platform.Table[name].x = Game.Platform.x+x
  Game.Platform.Table[name].y = Game.Platform.y+y
  Game.Platform.Table[name].x2 = Game.Platform.x+x2
  Game.Platform.Table[name].y2 = Game.Platform.y+y2
  Game.Platform.Table[name].img = img
  Game.Platform.Table[name].passable = passable
  Game.Platform.Table[name].downpassable = downpassable
end
function Game.Platform.drawBlock (name)
  if not Game.Platform.Table[name].passable then
    Collisions.update (name, Game.Platform.Table[name].x, Game.Platform.Table[name].y, Game.Platform.Table[name].x2, Game.Platform.Table[name].y2)
    Collisions.drawObject (name, Game.Platform.Table[name].img)
  else
    screen.blit(Game.Platform.scr, Game.Platform.Table[name].x, Game.Platform.Table[name].y, Game.Platform.Table[name].img)
  end
end
-- end Platform --